<% add_decidim_page_title(t("decidim.admin.titles.users")) %>
<div class="card">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t "decidim.admin.titles.users" %>
      <% if allowed_to? :create, :admin_user %>
        <%= link_to t("actions.user.new", scope: "decidim.admin"), [:new, :user], class: "button button__sm button__secondary" %>
      <% end %>
    </h1>
  </div>
  <%= admin_filter_selector %>
  <div class="table-stacked">
    <table class="table-list">
      <thead>
        <tr>
          <th><%= sort_link(query, :role, t("models.user.fields.role", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :name, t("models.user.fields.name", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :email, t("models.user.fields.email", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :invitation_sent_at, t("models.user.fields.invitation_sent_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :invitation_accepted_at, t("models.user.fields.invitation_accepted_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :last_sign_in_at, t("models.user.fields.last_sign_in_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :created_at, t("models.user.fields.created_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= t("decidim.admin.actions.actions") %></th>
        </tr>
      </thead>
      <tbody>
        <% @users.each do |user| %>
          <tr data-user-id="<%= user.id %>">
            <td data-label="<%= t("models.user.fields.role", scope: "decidim.admin") %>">
              <%= t("models.user.fields.roles.#{user.active_role}", scope: "decidim.admin") %>
            </td>
            <td data-label="<%= t("models.user.fields.name", scope: "decidim.admin") %>">
              <%= user.name %>
            </td>
            <td data-label="<%= t("models.user.fields.email", scope: "decidim.admin") %>">
              <%= user.email %>
            </td>
            <td data-label="<%= t("models.user.fields.invitation_sent_at", scope: "decidim.admin") %>">
              <%= l(user.invitation_sent_at, format: :short) if user.invitation_sent_at %>
            </td>
            <td data-label="<%= t("models.user.fields.invitation_accepted_at", scope: "decidim.admin") %>">
              <%= l(user.invitation_accepted_at, format: :short) if user.invitation_accepted_at %>
            </td>
            <td data-label="<%= t("models.user.fields.last_sign_in_at", scope: "decidim.admin") %>">
              <%= l(user.last_sign_in_at, format: :short) if user.last_sign_in_at %>
            </td>
            <td data-label="<%= t("models.user.fields.created_at", scope: "decidim.admin") %>">
              <%= l(user.created_at, format: :short) %>
            </td>
            <td data-label="<%= t("decidim.admin.actions.actions") %>" class="table-list__actions">
              <% if (allowed_to?(:invite, :admin_user, user: user) && user.invited_to_sign_up?) || (allowed_to? :destroy, :admin_user, user: user) %>
                <button type="button" data-controller="dropdown" data-target="actions-user-<%= user.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: user.name) %>">
                  <%= icon "more-fill", class: "text-secondary" %>
                </button>

                <div class="inline-block relative">
                  <ul id="actions-user-<%= user.id %>" class="dropdown dropdown__action" aria-hidden="true">
                    <% if allowed_to?(:invite, :admin_user, user: user) && user.invited_to_sign_up? %>
                      <li class="dropdown__item">
                        <%= link_to [:resend_invitation, user], method: :post, class: "dropdown__button" do %>
                          <%= icon "refresh-line" %>
                          <%= t("actions.resend_invitation", scope: "decidim.admin") %>
                        <% end %>
                      </li>

                      <hr>
                    <% end %>

                    <% if allowed_to? :destroy, :admin_user, user: user %>
                      <li class="dropdown__item">
                        <%= link_to user, method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") }, class: "dropdown__button dropdown__button--danger" do %>
                          <%= icon "delete-bin-line" %>
                          <%= t("actions.destroy", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% end %>
                  </ul>
                </div>
              <% end %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>
<%= decidim_paginate @users %>
